package com.example.chat.common.utils;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Path;

@Slf4j
public class MyFileUtils {

    public static Document loadFile(MultipartFile multipartFile, String filePath) {
        Document document = null;
        try {
            File tempFile = new File(filePath);
            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
                fos.write(multipartFile.getBytes());
            }

            Path path = tempFile.toPath();
            return FileSystemDocumentLoader.loadDocument(path);
        } catch (Exception e) {
            log.error("加载文件失败:{}", e.getMessage());
            return document;
        }
    }
}
